/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rw.ac.kiosc.jdbc.school.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 *
 * @author antoine
 */
public class SchoolSessionFactory {

    private static SessionFactory sessionFactory;
    private static Configuration configuration = new Configuration();
    private static String configFile = "rw/ac/kiosc/jdbc/school/hibernate/hibernate.cfg.xml";

    private SchoolSessionFactory() {
    }

    private static SessionFactory getInstance() {
        if (sessionFactory == null) {
            configuration.configure(configFile);
            sessionFactory = configuration.buildSessionFactory();
        }
        return sessionFactory;
    }

    public static Session getSession() {
        Session session = getInstance().getCurrentSession();
        if (session == null) {
            session = getInstance().openSession();
        }
        if (!session.isOpen()) {
            session = getInstance().openSession();
        }
        return session;
    }
}
